﻿using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Media;
using Java.Lang;
using Android.Views.InputMethods;
using Android.Content.PM;
using Hezi.App.com.xxxfeng.hezi;
using System.Threading.Tasks;

namespace Hezi.App
{
    [Activity(Label = "消息盒子", MainLauncher = true, Icon = "@drawable/heziIcon")]
    public class MainActivity : Activity
    {
        Button sub;
        EditText input;
        ScrollView scroll;
        public TextView box;
        InputMethodManager inputMethodManager;
        protected override void OnCreate(Bundle bundle)
        {

            base.OnCreate(bundle);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            sub = FindViewById<Button>(Resource.Id.submit);
            input = FindViewById<EditText>(Resource.Id.input);
            scroll = FindViewById<ScrollView>(Resource.Id.scroll);
            box = FindViewById<TextView>(Resource.Id.box);
            input.ClearFocus();
            //box.Focusable = true;
            inputMethodManager = (InputMethodManager)GetSystemService(Context.InputMethodService);
            AddMsg("*输入help获取命令列表");

            Common.AppInit();
            var version = Common.getVersionCode(this);
            if (!string.IsNullOrEmpty(AppCache.userName))
            {
                Intent intent = new Intent(this, typeof(MsgService));
                StartService(intent);
            }
            else
            {
                AddMsg("*请输入:user 用户名 来创建你的帐号");
            }

            Task<PingModel> updateTask = new Task<PingModel>(CheckUpdate);
            updateTask.ContinueWith((t) =>
            {
                var r = t.Result;
                if (r != null && r.State != 0)
                {
                    AddMsg("*系统有更新,输入update更新APP");
                }
            });
            updateTask.Start();
            sub.Click += Sub_Click;
        }
        /// <summary>
        /// 提交按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Sub_Click(object sender, EventArgs e)
        {
            string command = input.Text;
            if (!string.IsNullOrWhiteSpace(command))
            {
                Run(command);
            }
            Close();
            //int offset = box.MeasuredHeight - scroll.MeasuredHeight;
            int offset = box.Height - scroll.Height;
            if (offset > 0) scroll.ScrollTo(0, offset);
        }
        /// <summary>
        /// 执行命令
        /// </summary>
        /// <param name="commandText"></param>
        public void Run(string commandText)
        {
            AddMsg("#" + commandText);
            Commander.Command(this, commandText, AddMsg);
        }
        /// <summary>
        /// 清空输入框,并收起输入法
        /// </summary>
        public void Close()
        {

            input.SetText("", TextView.BufferType.Normal);
            inputMethodManager.HideSoftInputFromWindow(input.WindowToken, HideSoftInputFlags.None);
        }
        /// <summary>
        /// 文本框添加消息
        /// </summary>
        /// <param name="text"></param>
        public void AddMsg(string text)
        {
            box.SetText(box.Text + "\n" + text, TextView.BufferType.Normal);
        }
        public PingModel CheckUpdate()
        {
            try
            {
                return WcfService.CheckVersion(Common.getVersionCode(this));
                //return WcfService.CheckVersion("1.1");
            }
            catch(System.Exception)
            {
                return null;
            }
            
        }
    }
}

